<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<body>
	
</body>
<script>
	function Set(){
		var items={};
		/*this.has=function(value){
			return value in items;
		}*/
		//判断是否有这个属性
		this.has=function(value){
			return items.hasOwnProperty(value);
		};
		//添加属性
		this.add=function(value){
			if(!this.has(value)){
				items[value]=value;
				return true;
			}

			return false;
		};
		//移除属性
		this.remove=function(value){
			if(this.has(value)){
				delete items[value];
				return true;
			}
			return false;
		};
		//清空集合
		this.clear=function(){
			items={};
		};
		//获取length的方法
		this.size=function(){
			return Object.keys(items).length;
		};

		//另外一种获取length的方法
		this.sizeLegacy=function(){
			var count=0;
			for(var prop in items){
				if(items.hasOwnProperty(prop)){
					++count;
				}
			}

			return count;
		};
		//获取所有属性
		this.values=function(){
			return Object.keys(items);
		};
		//另外一种获取属性的方法
		this.valueLegacy=function(){
			var keys=[];
			for(var key in items){
				if(items.hasOwnProperty(key)){
					keys.push(key);
				}
			}
			return keys;
		};
		//并集
		this.union=function(otherSet){
			var unionSet=new Set();
			var values=this.values();
			for(var i=0;i<values.length;i++){
				unionSet.add(values[i]);
			}

			values=otherSet.values();
			for(var i=0;i<values.length;i++){
				unionSet.add(values[i]);
			}

			return unionSet;
		};
		//交集
		this.intersection=function(otherSet){
			var intersectionSet=new Set();
			var values=this.values();
			for(var i=0;i<values.length;i++){
				if(otherSet.has(values[i])){
					intersectionSet.add(values[i]);
				}
			}

			return intersectionSet;
		};
		//差集
		this.difference=function(otherSet){
			var differenceSet=new Set();
			var values=this.values();
			for(var i=0;i<values.length;i++){
				if(!otherSet.has(values[i])){
					differenceSet.add(values[i]);
				}
			}

			return differenceSet;
		};


	};
	//测试差集
	var setA=new Set();
	setA.add(1);
	setA.add(2);
	setA.add(3);

	var setB=new Set();
	setB.add(3);
	setB.add(4);
	setB.add(5);
	setB.add(6);

	var differenceAB=setA.difference(setB);
	console.log(differenceAB.values());


</script>
</html>